package com.xpxrule.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FieldUtil {
    public static class TIME_FORMAT_REGEX {
        public static final String YYYY_MM_DD_HH_MM_SS = "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$";
        public static final String YYYY_MM_DD = "^\\d{4}-\\d{2}-\\d{2}$";
        public static final String YYYYMMDDHHMMSS = "^\\d{4}/\\d{2}/\\d{2} \\d{2}:\\d{2}:\\d{2}$";
        public static final String YYYYMMDD = "^\\d{4}/\\d{2}/\\d{2}$";
    }

    public static class TIME_FORMAT {
        public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
        public static final String YYYY_MM_DD = "yyyy-MM-dd";
        public static final String YYYYMMDDHHMMSS = "yyyy/MM/dd HH:mm:ss";
        public static final String YYYYMMDD = "yyyy/MM/dd";
    }

    public static boolean isLegalDateFormat(String dataStr) {
        if (dataStr.matches(TIME_FORMAT_REGEX.YYYY_MM_DD_HH_MM_SS)) {
            return true;
        } else if (dataStr.matches(TIME_FORMAT_REGEX.YYYY_MM_DD)) {
            return true;
        } else if (dataStr.matches(TIME_FORMAT_REGEX.YYYYMMDDHHMMSS)) {
            return true;
        } else if (dataStr.matches(TIME_FORMAT_REGEX.YYYYMMDD)) {
            return true;
        } else if (FieldUtil.isTimestamp(dataStr)) {
            return true;
        } else {
            return false;
        }
    }
    public static boolean isTimestamp(String str) {
        long timestamp = Long.parseLong(str);
        long minTimestamp = 0L;
        if (str.length() == 10) {
            timestamp *= 1000;
        } else if (str.length() != 13) {
            return false;
        }
        return timestamp >= minTimestamp;
    }

    public static Date parseStringToDate(String dateStr) {
        try {
            SimpleDateFormat formatter =null;
            if (dateStr.matches(TIME_FORMAT_REGEX.YYYY_MM_DD_HH_MM_SS)) {
                formatter = new SimpleDateFormat(TIME_FORMAT.YYYY_MM_DD_HH_MM_SS);
                formatter.setLenient(false);
                return formatter.parse(dateStr);
            } else if (dateStr.matches(TIME_FORMAT_REGEX.YYYY_MM_DD)) {
                formatter = new SimpleDateFormat(TIME_FORMAT.YYYY_MM_DD);
                formatter.setLenient(false);
                return formatter.parse(dateStr);
            } else if (dateStr.matches(TIME_FORMAT_REGEX.YYYYMMDDHHMMSS)) {
                formatter = new SimpleDateFormat(TIME_FORMAT.YYYYMMDDHHMMSS);
                formatter.setLenient(false);
                return formatter.parse(dateStr);
            } else if (dateStr.matches(TIME_FORMAT_REGEX.YYYYMMDD)) {
                formatter = new SimpleDateFormat(TIME_FORMAT.YYYYMMDD);
                formatter.setLenient(false);
                return formatter.parse(dateStr);
            } else if (FieldUtil.isTimestamp(dateStr)) {
                // 时间戳转为Date类型
                return new Date(dateStr.length() == 10 ? Long.parseLong(dateStr) * 1000 : Long.parseLong(dateStr));
            } else {
                return null;
            }
        } catch (ParseException e) {
            return null;
        }
    }
}
